package com.qqin.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qqin.pojo.Goods;
import com.qqin.pojo.PageBean;
import com.qqin.pojo.Result;
import com.qqin.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Slf4j
@CrossOrigin
@RestController//直接返回 JSON 或 XML 等格式的数据
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    //根据商品ID查询商品
    @GetMapping("/{goodsId}")
    public Result selectById(@PathVariable Integer goodsId) {
        log.info("根据ID查询商品");
        Goods goods = goodsService.selectById(goodsId);

        return Result.success(goods);
    }

    //分页查询所有商品
    @GetMapping("/page")
    public Result page(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("分页查询所有商品");
        PageBean pageBean = goodsService.page(page, pageSize);

        return Result.success(pageBean);
    }

    //查询所有待售商品
    @GetMapping("/listed")
    public Result selectByListed() {
        log.info("查询所有待售商品");
        List<Goods> goods = goodsService.selectByListed();

        return Result.success(goods);
    }

    //根据条件搜索商品
    @GetMapping("/condition")
    public Result selectByCondition(String goodsName) {
        log.info("搜索商品");
        List<Goods> goods = goodsService.selectByCondition(goodsName);

        return Result.success(goods);
    }

    //随机选取商品图片
    @GetMapping("/photo")
    public Result getPhoto(@RequestParam(defaultValue = "4") Integer photoNum) {
        log.info("随机选取商品图片");
        List<Goods> photos = goodsService.getPhoto(photoNum);

        return Result.success(photos);
    }

    //添加商品
    @PostMapping("/add")
    public Result add(@RequestBody Map<String, Object> map) throws JsonProcessingException {
        log.info("发布商品");
        ObjectMapper objectMapper = new ObjectMapper();// Jackson 库中的一个类，用于处理 JSON 数据的序列化和反序列化。


        String goodsJson = objectMapper.writeValueAsString(map.get("goods"));//将 Map 中的值转换为 JSON 字符串
        String userIdJson = objectMapper.writeValueAsString(map.get("userId"));//将 JSON 字符串反序列化为指定类型的 Java 对象。

        Goods goods = objectMapper.readValue(goodsJson, Goods.class);
        Integer userId = objectMapper.readValue(userIdJson, Integer.class);

        goodsService.add(goods, userId);

        return Result.success();
    }

    //更新商品
    @PutMapping("/update")
    public Result updateById(@RequestBody Goods goods) {
        log.info("修改商品信息");
        Goods g = goodsService.updateById(goods);

        return Result.success(g);
    }

    //下架商品
    @GetMapping("/delisting/{goodsId}")
    public Result delisting(@PathVariable Integer goodsId) {
        log.info("下架商品");
        goodsService.delisting(goodsId);

        return Result.success();
    }

    //上传图片
    @PostMapping("/upload")
    public Result upload(MultipartFile photo) throws IOException {
        log.info("上传图片");
        String path = goodsService.upload(photo);

        return Result.success(path);
    }

}
